{% extends "PersonCenter/base.html" %}

{% block title %}修改收货地址{% endblock %}

{% block file %}
    <link rel="stylesheet" href="{{ url_for('static',filename='css/module.css') }}">
    <link rel="stylesheet" href="{{ url_for('static',filename='css/userdata.css') }}">
    <link rel="stylesheet" href="{{ url_for('static',filename='css/index.css') }}">
    <link href="{{ url_for('static',filename='css/folder/bootstrap.css') }}" rel="stylesheet" type="text/css">
    <script src="{{ url_for('static',filename='js/holder.js') }}"></script>
    <script src="{{ url_for('static',filename='css/folder/jquery-1.9.1.min.js') }}"></script>
    <script src="{{ url_for('static',filename='css/folder/bootstrap.js') }}"></script>
    <script type="text/javascript" src="{{ url_for('static',filename='js/jquery-3.6.0.min.js') }}"></script>
    <script type="text/javascript" src="{{ url_for('static',filename='js/jquery-getUrlParam.js') }}"></script>
{% endblock %}

{% block body %}
<!--返回-->
<div id="test">
    <span id="sp11"><a href="{{ url_for("index.index") }}">首页</a></span>&nbsp;>
    <span id="sp22">管理</span>&nbsp;>
    <span id="sp33"><a href="password.html">修改地址</a></span>
</div>
<!--中间卡片-->
<div class="container">
    <div class="col-md-2">
        <!--左侧导航开始-->
        <div class="panel-group" id="accordion">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <!--主选项：资料修改-->
                    <p class="panel-title">
                        <a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">资料修改</a>
                    </p>
                </div>
                <div id="collapseTwo" class="panel-collapse collapse in">
                    <div class="panel-body">
                        <div><a href="{{ url_for("person.changePass") }}"><b>修改密码</b></a></div>
                        <div><a href="{{ url_for("person.userInfo") }}" style="font-weight: bold">个人资料</a></div>
                        <div><a href="{{ url_for("person.showAdd") }}" style="font-weight: bold;color: black">收货管理</a></div>
                    </div>
                </div>
            </div>
        </div>
        <!--左侧导航结束-->
    </div>
    <div class="col-md-10">
        <div class="panel panel-default">
            <ul class="nav nav-tabs">
                <li><a href="{{ url_for("person.changePass") }}">修改密码</a></li>
                <li><a href="{{ url_for("person.userInfo") }}">个人资料</a></li>
                <li class="active"><a href="{{ url_for("person.showAdd") }}">收货管理</a></li>
            </ul>
            <div class="panel-body">
                <!--新增收货地址表单开始-->
                <form id="form-add-new-address" class="form-horizontal" role="form">
                    <div class="form-group">
                        <label class="col-md-2 control-label"><span class="txtred">*</span>收货人：</label>
                        <div class="col-md-8">
                            <input id="shouhuoname" name="name" type="text" class="form-control" placeholder="请输入收货人姓名">
                        </div>
                    </div>
                    <div class="address_content" data-toggle="distpicker">
                        <div class="form-group">
                            <label class="col-md-2 control-label"><span class="txtred">*</span>省/直辖市：</label>
                            <div class="col-md-3">
                                <select id="province-list" name="provinceCode" class="form-control" data-province="---- 选择省 ----"></select>
                            </div>
                            <label class="col-md-2 control-label"><span class="txtred">*</span>城市：</label>
                            <div class="col-md-3">
                                <select id="city-list" name="cityCode" class="form-control" data-city="---- 选择市 ----"></select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-md-2 control-label"><span class="txtred">*</span>区县：</label>
                            <div class="col-md-3">
                                <select id="area-list" name="areaCode" class="form-control" data-district="---- 选择区 ----"></select>
                            </div>
                            <label class="col-md-2 control-label">邮政编码：</label>
                            <div class="col-md-3">
                                <input id="youzhengbianma"  name="zip" type="text" class="form-control" placeholder="请输入邮政编码">
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-md-2 control-label"><span class="txtred">*</span>详细地址：</label>
                        <div class="col-md-8">
                            <textarea id="detailaddress" name="address" class="form-control" rows="3" placeholder="输入详细的收货地址，小区名称、门牌号等"></textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-md-2 control-label"><span class="txtred">*</span>手机：</label>
                        <div class="col-md-3">
                            <input id="phonenum" name="phone" type="text" class="form-control" placeholder="请输入手机号码">
                        </div>
                        <label class="col-md-2 control-label">固话：</label>
                        <div class="col-md-3">
                            <input id="gudingtel" name="tel" type="text" class="form-control" placeholder="请输入固定电话号码">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-md-2 control-label">地址类型：</label>
                        <div class="col-md-8">
                            <input id="tag" name="tag" type="text" class="form-control" placeholder="请输入地址类型，如：家、公司或者学校">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-md-2 control-label"><a href="{{ url_for("person.showAdd") }}">返回</a>&nbsp;&nbsp;&nbsp;</label>
                        <div class="col-sm-10">
                            <input id="btn-add-new-address" type="button" class="col-md-1 btn btn-primary" value="修改" />
                            <input type="reset" class="col-md-offset-1 col-md-1 btn btn-primary" value="重置" />
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    //页面加载显示地址详情
    $(document).ready(function () {
        $.ajax({
            url:"/person/change_address",
            type:"post",
            dataType:"json",
            contentType: 'application/json',
            data:JSON.stringify({"aid":$.getUrlParam("aid")}),
            success:function (json) {
                let data=json.data;
                console.log(json.data);
                $("#shouhuoname").val(data.name);
                showProvinceList();
                let defaultprovince="<option value="+data.provinceCode+">"+data.provinceName+"</option>";
                let defaultcity="<option value="+data.cityCode+">"+data.cityName+"</option>";
                let defaultarea="<option value="+data.areaCode+">"+data.areaName+"</option>";
                $("#province-list").append(defaultprovince);
                // $("#province-list").value=data.provinceCode;
                $("#city-list").append(defaultcity);
                // $("#city-list").value=data.cityCode;
                $("#area-list").append(defaultarea);
                // $("#area-list").value=data.areaCode;
                $("#youzhengbianma").val(data.zip);
                $("#detailaddress").val(data.address);
                $("#phonenum").val(data.phone);
                $("#gudingtel").val(data.tel);
                $("#tag").val(data.tag);
            }
        });
    });
</script>
<script type="text/javascript">
    let defaultOption="<option value='0'>---- 请选择 ----</option>"

    /*显示市列表*/
    $("#province-list").change(function () {
        /*获取行政区父代码*/
        let parent=$("#province-list").val();
        /*清空select下拉列表option元素*/
        $("#city-list").empty();
        $("#area-list").empty();
        /*填充默认值*/
        $("#city-list").append(defaultOption);
        $("#area-list").append(defaultOption);
        if(parent==0){
            return;
        }
        $.ajax({
            url:"/person/districts",
            type:"post",
            contentType: 'application/json',
            dataType: "json",
            data:JSON.stringify({"parent":parent}),
            success:function (json) {
                if(json.state==200){
                    let list=json.data;
                    for(let i=0;i<list.length;i++){
                        let opt="<option value='"+list[i].code+"'>"+list[i].name+"</option>";
                        $("#city-list").append(opt);
                    }
                }else {
                    alert("城市加载失败！");
                }
            }
        });
    });

    /*显示区列表*/
    $("#city-list").change(function () {
        /*获取行政区父代码*/
        let parent=$("#city-list").val();
        /*清空select下拉列表option元素*/
        $("#area-list").empty();
        /*填充默认值*/
        $("#area-list").append(defaultOption);
        if(parent==0){
            return;
        }
        $.ajax({
            url:"/person/districts",
            type:"post",
            contentType: 'application/json',
            dataType: "json",
            data:JSON.stringify({"parent":parent}),
            success:function (json) {
                if(json.state==200){
                    let list=json.data;
                    for(let i=0;i<list.length;i++){
                        let opt="<option value='"+list[i].code+"'>"+list[i].name+"</option>";
                        $("#area-list").append(opt);
                    }
                }else {
                    alert("县区加载失败！");
                }
            }
        });
    });

    /*显示省列表*/
    function showProvinceList() {
        $.ajax({
            url: "/person/districts",
            type: "post",
            contentType: 'application/json',
            dataType: "json",
            data: JSON.stringify({"parent":"86"}),
            success:function (json) {
                if(json.state==200){
                    let list=json.data;
                    for(let i=0;i<list.length;i++){
                        let opt="<option value='"+list[i].code+"'>"+list[i].name+"</option>";
                        $("#province-list").append(opt);
                    }
                }else {
                    alert("省市加载失败！");
                }
            }
        });
    }
</script>
<script type="text/javascript">
    layui.use('layer', function(){
        var layer = layui.layer;

        //点击修改地址按钮
        $("#btn-add-new-address").click(function () {
            console.log($("#form-add-new-address").serialize())
            $.ajax({
                url:"/person/change/"+$.getUrlParam("aid"),
                type:"post",
                data:$("#form-add-new-address").serialize(),
                dataType:"json",
                success:function (json) {
                    if(json.state==200){
                        console.log($("#form-add-new-address").serialize());
                        layer.open({
                            title: '提示'
                            ,content: json.msg
                        });
                    }else {
                        layer.open({
                            title: '提示'
                            ,content: json.msg
                        });
                    }
                }
            });
        });
    });
</script>
{% endblock %}